import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.LinkedList;

import javax.imageio.ImageIO;


// Project: 

/******************************************************************************
 * <p>
 * File: cropTileset.java
 * </p>
 * @author  Tadhg O'Sullivan, rogue
 * @author <a href="mailto:tadhg.osullivan@ucdconnect.ie">tadhg.osullivan@ucdconnect.ie</a>
 * @version 1.0, 2007
 * @created 20-Feb-07 6:06:06 PM
 * @revised 21-Feb-07
 * Copyright 2007 Tadhg O'Sullivan
 * <p>
 * Description: assigns coordinates to each tileObject and places them accordingly 
 * into a 2D array, returns a 2D array
 * </p>
 *  
 *  This file is part of Retribution.
 *
 *  Retribution is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  Retribution is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Retribution; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *   
 *****************************************************************************/

public class cropTileset {
	
	protected LinkedList<mapTile> ml;
	protected LinkedList<hero> hl;
	protected LinkedList<enemy> el;
	protected LinkedList<npc> nl;
	protected BufferedImage imgMap = null;
	protected BufferedImage imgHero = null;
	protected BufferedImage imgEnemy = null;
	protected BufferedImage imgNpc = null;
	protected mapTile newTile;
	protected hero newTile2;
	protected enemy newTile3;
	protected npc newTile4;
	
	/**
	 * @param startTile The first tile to crop from the tileset
	 * @param endTile The final tile to crop from the tileset
	 * @return ml Returns a LinkedList filled with map tiles
	 */
	
	public LinkedList<mapTile> importMapTileset(int startTile, int endTile){
		ml = new LinkedList<mapTile>();
		int count = 1;
		boolean collision = false;
		boolean goal = false;
		int house = 1;
		
		try {
			// get the tileset BufferedImage
		    imgMap = ImageIO.read(this.getClass().getResource("Tiles3.bmp"));
		} catch (IOException e) {
		}
		
		// for loop to crop the individual tiles and set their collisionState and goal booleans
		for(int i = (startTile * 8); i < (endTile * 8); i += 8){
			if((count == 3) || (count == 8) || (count == 12) || (count == 18) || (count == 20) || (count == 21) || (count == 22)){
				collision  = false;
			}else{
				collision = true;
			}
			if((count == 12) || (count == 18) || (count == 20) || (count == 21) || (count == 22)){
				goal = true;
			}else{
				goal = false;
			}
			if((count == 18) || (count == 20)){
				newTile = new mapTile(imgMap.getSubimage(i, 0, 8, 8), count, 0, 0, collision, goal, house);
				house++;
			}else{
			newTile = new mapTile(imgMap.getSubimage(i, 0, 8, 8), count, 0, 0, collision, goal, 0);
			}
			//System.out.println( newTile.getGoal());
			//System.out.println(newTile.getImage().toString());
			//System.out.println(newTile.getCollisionState());
			//System.out.println(newTile.getNum());
			ml.add(newTile);
			//System.out.println("count"+count);
			count++;
		}
		return ml;
	}
	
	/**
	 * @param startTile The first tile to crop from the tileset
	 * @param endTile The final tile to crop from the tileset
	 * @return hl Returns a LinkedList filled with hero tiles
	 */
	
	public LinkedList<hero> importHeroTileset(int startTile, int endTile){
		hl = new LinkedList<hero>();
		int count = 1;
		
		try {
			// get the tileset BufferedImage
		    imgHero = ImageIO.read(this.getClass().getResource("herobk.gif"));//"herobk.gif"));
		} catch (IOException e) {
		}
		
		// for loop to crop the individual tiles and set their collisionState and goal booleans
		for(int i = (startTile * 32); i < (endTile * 32); i += 32){
			newTile2 = new hero(imgHero.getSubimage(i, 0, 32, 32), count, 0, 0, 0, 0, false);
			//System.out.println( newTile.getGoal());
			//System.out.println(newTile.getImage().toString());
			//System.out.println(newTile.getCollisionState());
			//System.out.println(newTile.getNum());
			hl.add(newTile2);
			count++;
		}
		return hl;
	}

	/**
	 * @param startTile The first tile to crop from the tileset
	 * @param endTile The final tile to crop from the tileset
	 * @return he Returns a LinkedList filled with enemy tiles
	 */
	
	public LinkedList<enemy> importEnemyTileset(int startTile, int endTile, String type){
		el = new LinkedList<enemy>();
		int count = 1;
		
		try { 
			// get the tileset BufferedImage
		    imgEnemy = ImageIO.read(this.getClass().getResource(type));
		} catch (IOException e) {
		}
		
		// for loop to crop the individual tiles and set their collisionState and goal booleans
		for(int i = (startTile * 32); i < (endTile * 32); i += 32){
			newTile3 = new enemy(imgEnemy.getSubimage(i, 0, 32, 32), count, 0, 0, 0, 0, false, type);
			//System.out.println( newTile.getGoal());
			//System.out.println(newTile.getImage().toString());
			//System.out.println(newTile.getCollisionState());
			//System.out.println(newTile.getNum());
			el.add(newTile3);
			count++;
		}
		return el;
	}
	
	/**
	 * @param startTile The first tile to crop from the tileset
	 * @param endTile The final tile to crop from the tileset
	 * @return ml Returns a LinkedList filled with map tiles
	 */
	
	public LinkedList<mapTile> importHouseTileset(int startTile, int endTile){
		ml = new LinkedList<mapTile>();
		int count = 1;
		boolean collision = false;
		boolean goal = false;
		
		try {
			// get the tileset BufferedImage
		    imgMap = ImageIO.read(this.getClass().getResource("house.bmp"));
		} catch (IOException e) {
		}
		
		// for loop to crop the individual tiles and set their collisionState and goal booleans
		for(int i = (startTile * 8); i < (endTile * 8); i += 8){
			if((count == 10) || (count == 11) || (count == 12) || (count == 13)){
				collision  = true;
			}else{
				collision = false;
			}
			if((count == 2) ){
				goal = true;
			}else{
				goal = false;
			}
			newTile = new mapTile(imgMap.getSubimage(i, 0, 8, 8), count, 0, 0, collision, goal, 0);
			//System.out.println( newTile.getGoal());
			//System.out.println(newTile.getImage().toString());
			//System.out.println(newTile.getCollisionState());
			//System.out.println(newTile.getNum());
			ml.add(newTile);
			//System.out.println("count"+count);
			count++;
		}
		return ml;
	}
	
	/**
	 * @param startTile The first tile to crop from the tileset
	 * @param endTile The final tile to crop from the tileset
	 * @return nl Returns a LinkedList filled with hero tiles
	 */
	
	public LinkedList<npc> importNpcTileset(int startTile, int endTile, String type){
		nl = new LinkedList<npc>();
		int count = 1;
		
		try {
			// get the tileset BufferedImage
		    imgNpc = ImageIO.read(this.getClass().getResource(type));
		} catch (IOException e) {
		}
		
		
//		 for loop to crop the individual tiles and set their collisionState and goal booleans
		for(int i = (startTile * 32); i < (endTile * 32); i += 32){
			newTile4 = new npc(imgEnemy.getSubimage(i, 0, 32, 32), count, 0, 0, 0, 0, false, type);
			//System.out.println( newTile.getGoal());
			//System.out.println(newTile.getImage().toString());
			//System.out.println(newTile.getCollisionState());
			//System.out.println(newTile.getNum());
			nl.add(newTile4);
			count++;
		}
		return nl;
	}
	
}
